package cn.dengyonsheng.mail_send_service.entities;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.SimpleMailMessage;


@Data
public class MailMessage {
    private static final long serialVersionUID = 1L;
    /**
     * 接收邮箱
     */
    private String to;
    /**
     * 标题
     */
    private String subject;
    /**
     * 正文
     */
    private String text;

    public String toString() {
        ObjectMapper mapper = new ObjectMapper();
        try {
            return mapper.writeValueAsString(this);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

    public static MailMessage parseJson(String json) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        return mapper.readValue(json, MailMessage.class);
    }

    public SimpleMailMessage toSimpleMailMessage() {
        SimpleMailMessage msg = new SimpleMailMessage();
        msg.setText(this.getText());
        msg.setTo(this.getTo());
        msg.setSubject(this.getSubject());
        return msg;
    }
}

